package com.gome.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState;

/**
 * @author hemaoji
 * @since 1.0
 * 2010-2-18 下午08:57:36
 */
public class AbstractZooKeeper implements Watcher {
	private static final int SESSION_TIME 	= 2000;
	protected ZooKeeper  zooKeeper;
	protected CountDownLatch countDownLatch	= new CountDownLatch(1);

	public void connect(String hosts) throws IOException, InterruptedException{
		zooKeeper = new ZooKeeper(hosts,SESSION_TIME,this);
		countDownLatch.await();
	}
	public void process(WatchedEvent event) {
		if(event.getState() == KeeperState.SyncConnected){
			countDownLatch.countDown();
		}
	}
	public void close() throws InterruptedException{
		zooKeeper.close();
	}
}

